package com.huluobo.hyj.hyjmodel.ui.activity

import android.text.Editable
import android.text.TextWatcher
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.huluobo.hyj.hyjmodel.R
import com.huluobo.hyj.hyjmodel.adapter.EMMessageListenerAdapter
import com.huluobo.hyj.hyjmodel.adapter.MessageListAdapter
import com.huluobo.hyj.hyjmodel.contract.Chat.ChatContract
import com.huluobo.hyj.hyjmodel.contract.Chat.ChatPresenter
import com.hyphenate.chat.EMClient
import com.hyphenate.chat.EMMessage
import kotlinx.android.synthetic.main.activity_chat.*
import kotlinx.android.synthetic.main.header.*
import org.jetbrains.anko.toast

class ChatActivity :BaseActivity(),ChatContract.View {
    val Presenter= ChatPresenter(this)
    lateinit var username : String

    //验证消息
    private val messageListener = object : EMMessageListenerAdapter(){
        override fun onMessageReceived(messages: MutableList<EMMessage>?) {
            if (messages != null) {
                Presenter.addMessage(username,messages)
            }
            runOnUiThread { recyclerView.adapter?.notifyDataSetChanged() }
        }
    }

    //加载布局
    override fun getLayoutResId(): Int = R.layout.activity_chat
    override fun init() {
        super.init()
        initHeader()
        initEditText()
        initRecyclerView()
        EMClient.getInstance().chatManager().addMessageListener(messageListener)
        send.setOnClickListener {
            send()
        }

    }

    private fun initRecyclerView() {
        recyclerView.apply {
            setHasFixedSize(true)
            layoutManager = LinearLayoutManager(context)
            adapter = MessageListAdapter(context,Presenter.messages)
        }
    }

    private fun initEditText() {
        edit.addTextChangedListener(object :TextWatcher{
            override fun afterTextChanged(s: Editable?) {
                //输入完调用
                //如果用户文本输入长度大于0 发送按钮即可点击
                send.isEnabled = !s?.trim().isNullOrEmpty()
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
                //输入之前
                send.isEnabled = !s?.trim().isNullOrEmpty()
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }
        })
        edit.setOnEditorActionListener { v, actionId, event ->
            send()
            true
        }
    }
    private fun send(){
        hideSoftKeyboard()
        val message = edit.text.trim().toString()
        if(message.isNotEmpty()){
            Presenter.sendMessage(username,message)
        }
    }

    private fun initHeader() {
        back.visibility = View.VISIBLE
        back.setOnClickListener { finish() }
        //获取聊天的用户名
        username = intent.getStringExtra("username")
        val titleString = String.format(getString(R.string.chat_title),username)
        headerTitle.text = titleString
    }
    //开始发送
    override fun onStartSendMessage() {
        //通知RecyclerView刷新列表
        recyclerView.adapter?.notifyDataSetChanged()
    }
    //发送成功
    override fun onSendMessageSuccess() {
        recyclerView.adapter?.notifyDataSetChanged()
        toast(R.string.send_message_success)
        //清空编辑框
        edit.text.clear()
    }
    //发送失败
    override fun onSendMessageFailed() {
        recyclerView.adapter?.notifyDataSetChanged()
        toast(R.string.send_message_failed)
    }

}